#include <memory>
#include "TCPClient.hpp"
#include "Protocol.hpp"

void Work(int sockfd)
{
    // 获取输入
    int x, y;
    char oper;
    std::cout << "Please input x:";
    std::cin >> x;
    std::cout << "Please input y:";
    std::cin >> y;
    std::cout << "Please input oper:";
    std::cin >> oper;

    // 序列化
    Request req(x, y, oper);
    std::string package;
    req.Serialize(package);
    //封包
    std::string message;
    EnCode(package,&message);
    // 发送消息
    ::send(sockfd, message.c_str(), message.size(), 0);

    // 接收结果
    char buff[1024];
    int n = ::recv(sockfd, buff, sizeof(buff), 0);
    if (n > 0)
    {
        buff[n] = 0;
        std::string result = buff;
        //解包
        Decode(result,&message);
        // 反序列化
        Response res;
        res.Deserialize(message);
        LOG(LogLevel::DEBUG)<<"result:"<<res.Result()<<":code:"<<res.Code();
    }
}

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        std::cout << "Usgae Error" << std::endl;
        exit(-1);
    }
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);
    std::unique_ptr<TCPClient> c_ptr = std::make_unique<TCPClient>(Work,ip, port);
    c_ptr->Run();
    return 0;
}